package cn.edu.ctbu.simplescoremanagementsystem.webapi;

import cn.edu.ctbu.simplescoremanagementsystem.core.PageUtils;
import cn.edu.ctbu.simplescoremanagementsystem.domain.CourseScore;
import cn.edu.ctbu.simplescoremanagementsystem.domain.Student;
import cn.edu.ctbu.simplescoremanagementsystem.service.CourseScoreService;
import cn.edu.ctbu.simplescoremanagementsystem.service.StudentService;
import cn.edu.ctbu.simplescoremanagementsystem.utils.ResultUtils;
import cn.edu.ctbu.simplescoremanagementsystem.vo.Result;
import io.micrometer.common.util.StringUtils;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@Slf4j
@RestController
@RequestMapping("/webapi/coursescore")
public class CourseScoreRestController {
    @Autowired
    public  CourseScoreService courseScoreService;
    @Autowired
    public StudentService studentService;
    @GetMapping("/getByPage")
    public Result<PageUtils> getByPage(@RequestParam(value = "page",defaultValue = "0")Integer page,
                                       @RequestParam(value = "size",defaultValue = "7")Integer size,
                                       @RequestParam(value = "name",defaultValue = "")String name,
                                       @RequestParam(value = "no",defaultValue = "")String no,
                                       @RequestParam(value = "sno",defaultValue = "")String sno){
        Sort sort = Sort.by(Sort.Direction.ASC, "id");//排序
        Page<CourseScore> courseScorePage;
        Pageable pageable = PageRequest.of(page, size, sort);
        if (StringUtils.isEmpty(name) && StringUtils.isEmpty(no)&& StringUtils.isEmpty(sno)) {
            courseScorePage = courseScoreService.getByPage(pageable);
        } else {
            CourseScore courseScore = new CourseScore();
            ExampleMatcher matcher = ExampleMatcher.matching();
            if (!StringUtils.isEmpty(name)) {
                courseScore.setCname(name);
                matcher = matcher
                        .withMatcher("cname", ExampleMatcher.GenericPropertyMatchers.contains());//模糊查询包含，即%{username}%
            }
            if (!StringUtils.isEmpty(no)) {
                courseScore.setCno(no);
                matcher = matcher
                        .withMatcher("cno", ExampleMatcher.GenericPropertyMatchers.contains());//模糊查询包含，即%{username}%
            }
            if (!StringUtils.isEmpty(sno)) {
                courseScore.setSno(sno);
                matcher = matcher
                        .withMatcher("sno", ExampleMatcher.GenericPropertyMatchers.contains());//模糊查询包含，即%{username}%
            }

            Example<CourseScore> example = Example.of(courseScore,matcher);//模糊查询
            courseScorePage = courseScoreService.getByPage(example,pageable);

        }
        PageUtils pageUtils = new PageUtils(courseScorePage.getContent(),courseScorePage.getTotalElements());
        return ResultUtils.success(pageUtils);
    }

    @GetMapping("/getByPageStudent")
    public Result<PageUtils> getByPageStudent(@RequestParam(value = "page",defaultValue = "0")Integer page,
                                       @RequestParam(value = "size",defaultValue = "7")Integer size,
                                       @RequestParam(value = "name",defaultValue = "")String name,
                                       @RequestParam(value = "no",defaultValue = "")String no,
                                              HttpServletRequest httpRequest){
        Sort sort = Sort.by(Sort.Direction.ASC, "id");//排序
        Page<CourseScore> courseScorePage;
        Pageable pageable = PageRequest.of(page, size, sort);

        CourseScore courseScore = new CourseScore();
        ExampleMatcher matcher = ExampleMatcher.matching();
        HttpSession session = httpRequest.getSession();//获取session

        Student student = studentService.findById((Integer) session.getAttribute("student")).getData();//获取当前登录的学生
        courseScore.setSno(student.getSno());
        matcher = matcher.withMatcher("sno", ExampleMatcher.GenericPropertyMatchers.contains());//模糊查询包含，即%{username}%

        if (!StringUtils.isEmpty(name)) {
            courseScore.setCname(name);
            matcher = matcher
                    .withMatcher("cname", ExampleMatcher.GenericPropertyMatchers.contains());//模糊查询包含，即%{username}%
        }
        if (!StringUtils.isEmpty(no)) {
            courseScore.setCno(no);
            matcher = matcher
                    .withMatcher("cno", ExampleMatcher.GenericPropertyMatchers.contains());//模糊查询包含，即%{username}%
        }

        Example<CourseScore> example = Example.of(courseScore,matcher);//模糊查询
        courseScorePage = courseScoreService.getByPage(example,pageable);
        PageUtils pageUtils = new PageUtils(courseScorePage.getContent(),courseScorePage.getTotalElements());
        return ResultUtils.success(pageUtils);
    }

}
